<template>
  <div>
    姓名: <input type="text" v-model="ems.name"><br>
    年龄: <input type="text" v-model="ems.age"><br>
    生日: <input type="text" v-model="ems.bir"><br>
    地址: <input type="text" v-model="ems.address"><br>
    <button @click="updateEms">更新用户</button>
  </div>
</template>

<script>
export default {
  name: "Update",
  data() {
    return {
      id:"",
      name: "",
      age: "",
      bir: "",
      address: "",
      ems: {},//定义对象
    }
  },
  methods: {
    updateEms() {
      console.log(this.ems);
      this.$axios.post("http://localhost:8989/ems/updateEms",this.ems).then(res => {
        this.$router.push("/ems")
      }).catch(error => {
        console.log("更新失败")
      })
    },
    getEms(id) {
      console.log(id);
      this.$axios.get("http://localhost:8989/ems/queryEmsById?id=" + id).then(res => {
        // 发送请求 根据id查询用户信息 在查询成功后赋值给vue的data
        //this.ems = res.data;
        this.ems = res.data;
        this.name = res.data.name;
        this.age = res.data.age;
        this.bir = res.data.bir;
        this.address =res.data.address;
      }).catch(error => {
        alert("获取失败");
      })
    }
  },
    created() {
      //根据id查询用户信息
      let emsId = this.$route.params.id;
      this.getEms(emsId);
    }
}
</script>

<style scoped>

</style>
